import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import postcssPxtoRem from "postcss-pxtorem";
import path from "path";
import config from "./src/common/config"; // 引入配置文件
// https://vite.dev/config/
export default defineConfig({
  plugins: [
    react(),
    // [
    //   'import',
    //   {
    //     libraryName: 'react-vant',
    //     libraryDirectory: 'es',
    //     style: false,
    //   },
    // ],
  ],
  build: {
    target: "esnext", // 支持顶级await
  },
  css: {
    postcss: {
      plugins: [
        postcssPxtoRem({
          rootValue: config.postcssPxtoRemRootValue, // 按照自己的设计稿修改 1920/10
          propList: ["*"],
        }),
      ],
    },
  },
  resolve: {
    alias: {
      "@/views": path.resolve(__dirname, "src/views"),
      "@": path.resolve(__dirname, "src"), // 配置别名
    },
  },
  server: {
    host: "0.0.0.0",
    port: 3000, //设置默认启动端口号
    open: true, //自动打开浏览器
    proxy: {
      "/api": {
        target: "http://127.0.0.1:3000", // 目标服务器的地址
        changeOrigin: true, // 是否需要改变原始主机头为目标URL
        rewrite: (path) => path.replace(/^\/api/, ""), // 重写路径：去掉路径中的'/api'
      },
      // 可以添加多个代理规则
      "/newApi": {
        target: "http://127.0.0.1:3000",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/newApi/, ""),
      },
    },
  },
});
